import { ref } from 'vue'
import { getPlatforms } from '@/apis'
import type { PlatFormItem } from '@/apis'
import { formatObject } from '@/utils/common'

/** 平台模块 */
export function usePlatform() {
  const platformList = ref<PlatFormItem[]>([])

  const getPlatformList = async () => {
    const res = await getPlatforms()
    platformList.value = res.data
  }

  // 根据sourceType返回平台置顶字段
  const formatPlatform = (sourceType: number, key: 'title' | 'icon' = 'title') => {
    return formatObject(unref(platformList), 'id', sourceType, key)
  }

  return { platformList, getPlatformList, formatPlatform }
}
